﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;

namespace TireService.Mobile
{
    public partial class Container : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSearchTireSet_Click(object sender, EventArgs e)
        {
            

            TireServiceDB db = new TireServiceDB();
            try
            {
                if (rbSearchFilter.SelectedItem.Value.ToString() == "0")
                {
                    Customer customer = db.Customers.SingleOrDefault(p => p.CustomerId == Convert.ToInt32(txtSearchString.Text));

                    if (customer != null)
                    {
                        pTireSet.Visible = true;

                        lblName.Text = customer.Name;
                        lblAddress.Text = customer.Address;
                        lblZipCode.Text = customer.ZipCode;
                        lblCity.Text = customer.City;
                        lblAddress2.Text = customer.SecondaryAddress;
                        lblZipCode2.Text = customer.SecondaryZipCode;
                        lblCity2.Text = customer.SecondaryCity;
                        lblPhone1.Text = customer.Phone1;
                        lblPhone2.Text = customer.Phone2;
                        lblEmail.Text = customer.Email;
                    }

                    gvTireSets.DataSource = db.BusinessTireSetListViews.Where(p => p.CustomerId == customer.CustomerId).ToList();
                    gvTireSets.DataBind();
                    

                }
                if (rbSearchFilter.SelectedItem.Value.ToString() == "1")
                {
                    Car car = db.Cars.SingleOrDefault(p => p.RegistrationNumber == txtSearchString.Text);

                    if (car != null)
                    {
                        pTireSet.Visible = true;

                        Customer customer = db.Customers.SingleOrDefault(p => p.CustomerId == car.CustomerId);

                        if (customer != null)
                        {
                            lblName.Text = customer.Name;
                            lblAddress.Text = customer.Address;
                            lblZipCode.Text = customer.ZipCode;
                            lblCity.Text = customer.City;
                            lblAddress2.Text = customer.SecondaryAddress;
                            lblZipCode2.Text = customer.SecondaryZipCode;
                            lblCity2.Text = customer.SecondaryCity;
                            lblPhone1.Text = customer.Phone1;
                            lblPhone2.Text = customer.Phone2;
                            lblEmail.Text = customer.Email;
                        }

                        gvTireSets.DataSource = db.BusinessTireSetListViews.Where(p => p.CarId == car.CarId).ToList();
                        gvTireSets.DataBind();
                    }
                }
            }
            catch (Exception)
            {

            }

        }
    }
}